﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using eTruck.Helpers;
namespace eTruck.Views.Cars
{
    public partial class Form : System.Web.UI.Page
    {

        protected string returnValue = string.Empty;

        BLL.Car bll = new BLL.Car();
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = System.Web.HttpUtility.UrlDecode(Request["Type"]);
            if (type == "Save")
            {
                Save();
            }
            else
            {
                Exists();
            }
        }

        private void Save()
        {
            Model.Car model = new Model.Car();
           
            string CarID = System.Web.HttpUtility.UrlDecode(Request["CarID"]);
            model.Car_NO = System.Web.HttpUtility.UrlDecode(Request["CarNo"]);
           
            model.Capacity =decimal.Parse(System.Web.HttpUtility.UrlDecode(Request["Capacity"]).ToString());
            model.CorporationID =int.Parse(System.Web.HttpUtility.UrlDecode(Request["Corporation"]));
            model.Unit=System.Web.HttpUtility.UrlDecode(Request["Unit"]);
            model.Lastupdated = DateTime.Now;

            try
            {
                model.Userid = Convert.ToInt32(CookiesHelper.GetCookie(CommonHelper.LoginCookieName));
            }
            catch { }

            try
            {
                bool result = false;
                if (CarID.Length == 0)
                {
                    //create new User
                    result = bll.Add(model);
                }
                else
                {
                    //update User Info...
                    model.CarID = Convert.ToInt32(CarID);
                    result = bll.Update(model);
                }
                if (result)
                {
                    returnValue = "True";
                }
                else
                {
                    returnValue = "False";
                }
            }
            catch (Exception ex)
            {
                returnValue = ex.Message;
            }
        }

        private void Exists()
        {
            string CarNo = System.Web.HttpUtility.UrlDecode(Request["CarNo"]);
           
           DataSet ds=  bll.GetList("Car_NO='" + CarNo + "'");
           if (ds == null)
           {
               returnValue = "False";
           }
           else if (ds.Tables.Count == 0)
           {
               returnValue = "False";
           }
           else if (ds.Tables[0].Rows.Count == 0)
           {
               returnValue = "False";
           }
           else
           {
               returnValue = "True";
           }
              
        }
    }
}

